<template>
  <div>
    <el-form ref="addshopform" :rules="addRules" :model="packetData" label-width="120px">
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="packetData.name" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="面值" prop="price"> 
        <el-input v-model.number="packetData.price" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="选择类目" prop="goods_id" v-if="type == '4'"> 
        <el-select v-model="packetData.goods_id" placeholder="请选择" size="small" style="max-width:70%">
          <el-option
            v-for="item in goodTypeSub"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择商品" prop="category_id" v-if="type == '5'"> 
        <el-select v-model="packetData.category_id" placeholder="请选择" size="small" style="max-width:70%">
          <el-option
            v-for="item in goodsList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最小面值" prop="min_price" v-if="type == '3'"> 
        <el-input v-model.number="packetData.min_price" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="运气红包面值" prop="lucky_price" v-if="type == '3'"> 
        <el-input v-model.number="packetData.lucky_price" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="运气红包最小面值" prop="lucky_min_price" v-if="type == '3'"> 
        <el-input v-model.number="packetData.lucky_min_price" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="满减条件">
        <el-input v-model.number="packetData.full_price" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="可领取次数" prop="receive_count">
        <el-input v-model.number="packetData.receive_count" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="发行总量" prop="count">
        <el-input v-model.number="packetData.count" placeholder="请输入" size="small" style="max-width:70%"></el-input>
      </el-form-item>
      <el-form-item label="有效期" prop="from_date">
        <el-date-picker
          v-model="time"
          type="daterange"
          size="small" style="max-width: 70%">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态">
        <el-switch v-model="packetData.status" active-color="#13ce66" active-value="1" inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>

    <div style="text-align:center">
      <el-button type="primary" size="small" style="padding:8px 30px;" @click="submit">提 交</el-button>
    </div>
  </div>
</template>

<script>
// import store from '@/store'
import { parseTime } from '@/utils'
import { postCoupon, putCoupon } from '@/api/marketing'
import { getGoodsTypeSub, getGoodsList } from '@/api/goods'

export default {
  props: {
    isAdd: {// 是否是新增
      type: Boolean,
      required: true,
      default: true
    },
    packetData: {
      type: Object,
      required: true
    },
    type: {
      type: [Number,String],
      required: true
    }
  },
  mounted() {
    if(this.packetData.from_date&&this.packetData.to_date){
      this.time = [this.packetData.from_date,this.packetData.to_date]
    }  
    if(this.type === '4') {
      this.getGoodsTypeSub();
    } 
    if(this.type === '5') {
      this.getGoodsList();
    } 
  },
  data() {
    return {
      goodTypeSub: [],
      goodsList: [],
      time: [],
      addRules: {
        name: [
          { required: true, message: '请输入等级名称', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入面值', trigger: 'blur' },
          { type: 'number', message: '面值必须为数字', trigger: 'blur' }
        ],
        min_price: [
          { required: true, message: '请输入最小面值', trigger: 'blur' },
          { type: 'number', message: '最小面值必须为数字', trigger: 'blur' }
        ],
        lucky_price: [
          { required: true, message: '请输入运气红包面值', trigger: 'blur' },
          { type: 'number', message: '运气红包面值必须为数字', trigger: 'blur' }
        ],
        lucky_min_price: [
          { required: true, message: '请输入运气红包最小面值', trigger: 'blur' },
          { type: 'number', message: '运气红包最小面值必须为数字', trigger: 'blur' }
        ],
        receive_count: [
          { required: true, message: '请输入可领取次数', trigger: 'blur' },
          { type: 'number', message: '可领取次数必须为数字', trigger: 'blur' }
        ],
        count: [
          { required: true, message: '请输入总量', trigger: 'blur' },
          { type: 'number', message: '总量必须为数字', trigger: 'blur' }
        ],
        from_date: [
          { required: true, message: '请输入有效期', trigger: 'blur' },
        ],
        category_id: [
          { required: true, message: '请选择类目', trigger: 'blur' },
        ],
        goods_id: [
          { required: true, message: '请选择商品', trigger: 'blur' },
        ]
      }
    }
  },
  watch: {
    time(value) {
      if(value) {
        this.packetData.from_date = parseTime(value[0],'{y}-{m}-{d}');
        this.packetData.to_date = parseTime(value[1],'{y}-{m}-{d}');
      }else{
        this.packetData.from_date = ''
        this.packetData.to_date = ''
      }
    }
  },
  methods: {
    /**
     * 提交
     */
    submit() {
      this.$refs.addshopform.validate((valid) => {
        if(valid) {
          this.packetData.type = this.type;
          if(this.isAdd){
            postCoupon(this.packetData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('添加成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }else{
            putCoupon(this.packetData).then(response => {
              if(response.status === 200){
                this.$emit('success');
                this.$message.success('修改成功！');
              }else{
                this.$message.error(response.message);
              }
            })
          }
        }
      })    
    },
    /**
     * 获取商品类目
     */
    getGoodsTypeSub() {
      getGoodsTypeSub({key: this.$store.state.app.activeApp.saa_key}).then(res => {
        if(res.status === 200) {
          this.goodTypeSub = res.data;
          this.packetData.category_id = this.goodTypeSub[0].id;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 获取商品列表
     */
    getGoodsList() {
      getGoodsList({key: this.$store.state.app.activeApp.saa_key}).then(res => {
        if(res.status === 200) {
          this.goodsList = res.data;
          this.packetData.goods_id = this.goodsList[0].id;
        }else{
          this.$message.error(res.message);
        }
      })
    }
  },
}
</script>